#!/usr/bin/env python

###
# Wifi + GPS fingerprint collection tool
# Copyright (c) 2008, Yura Zenevich, Jorge Silva, and Jamon Camisso
# Homepage http://wiki.scyp.atrc.utoronto.ca/wiki/WiFiPositioning
# Contact scyp@atrc.utoronto.ca
#    
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program, see enclosed file gpl-3.txt or browse to
# <http://www.gnu.org/licenses/gpl.txt>
###

import bluetooth
import string
import os

'''
	the following looks for serial enabled bluetooth devices
	since we're using the holux unit exclusively right now, we don't need to scan
	this speeds things up considerably, so use a hardcoded MAC address where possible
'''

def gps_scan():
  #GPS_DEVICES = bluetooth.find_service(name = 'SPP slave')

  '''choose the first device in the list, in this case the HOLUX-M1000, mac is 00:1B:C1:00:F9:AE, the order may vary from system to system'''
  #DEVICE = GPS_DEVICES[0]
  #PORT = DEVICE['port']
  #HOST = DEVICE['host']
  HOST = '00:1B:C1:00:F9:AE'
  PORT = 1

  socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
  try:
  	socket.connect((HOST, PORT))
  except: 
  	return 0, 0, 0, 0

  '''
	convert from minutes and seconds to decimal, a normal GPGGA sentence will be in the form 1234.1234 meaning 12*, 34 minutes, 12.34 seconds
	we want decimal, so first divide by 100 to get lat/long whole number, then convert the minutes and seconds to decimal and add the two
  '''
  def dec_lat(LAT, LAT_H):
    if LAT_H == 'S':
      LAT = -LAT
    LAT_DEG = int(LAT/100)
    LAT_MIN = (LAT - LAT_DEG*100) / 60
    LAT_DEC = LAT_DEG + LAT_MIN
    return LAT_DEC

  '''
	  same comment applies here as to dec_lat
  '''
  def dec_long(LONG, LONG_H):
    if LONG_H == 'W':
      LONG = -LONG
    LONG_DEG = int(LONG/100)
    LONG_MIN = (LONG - LONG_DEG*100) / 60
    LONG_DEC = LONG_DEG + LONG_MIN
    return LONG_DEC

  i = 0
  SAT_NUM = 0
  FIX_DATA = 0
  while (i < 5): #do 5 scans, arbitrarily determined number
    GPGGA_RAW = socket.recv(1024).splitlines()
    print GPGGA_RAW
    if GPGGA_RAW[0].startswith('$'):
	  GPSDATA = GPGGA_RAW[0].split(',') 	#bust up sentences for easy parsing
	  try:
		  LAT = float(GPSDATA[2])			#latitude is in xxxx.xxxx form
		  LAT_H = GPSDATA[3]			#N/S hemisphere
		  SAT_NUM = GPSDATA[7]
		  FIX_DATA = GPSDATA[6]
		  DEC_LAT =  dec_lat(LAT, LAT_H)
	  except:
		  print "Can't get a valid GPGGA sentence"
  
	  try:
		  LONG = float(GPSDATA[4])		#longitude is in xxxx.xxxx form
		  LONG_H = GPSDATA[5]			#E/W hemispehere
		  DEC_LONG = dec_long(LONG, LONG_H)
		  #print "Longitude: ", GPSDATA[4], GPSDATA[5]
		  #print "Dec. Long: ", DEC_LONG
	  except:
		  print "Can't get a valid GPGGA sentence"

    i = i + 1
  
  return DEC_LAT, DEC_LONG, FIX_DATA, SAT_NUM
